package com.ibigcat.module.mdm.controller.admin.customergroup;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.ibigcat.framework.common.pojo.PageParam;
import com.ibigcat.framework.common.pojo.PageResult;
import com.ibigcat.framework.common.pojo.CommonResult;
import com.ibigcat.framework.common.util.object.BeanUtils;
import static com.ibigcat.framework.common.pojo.CommonResult.success;

import com.ibigcat.framework.excel.core.util.ExcelUtils;

import com.ibigcat.framework.apilog.core.annotation.ApiAccessLog;
import static com.ibigcat.framework.apilog.core.enums.OperateTypeEnum.*;

import com.ibigcat.module.mdm.controller.admin.customergroup.vo.*;
import com.ibigcat.module.mdm.dao.entity.CustomerGroupDO;
import com.ibigcat.module.mdm.service.CustomerGroupService;

@Tag(name = "管理后台 - 客户组管理")
@RestController
@RequestMapping("/mdm/customer-group")
@Validated
public class CustomerGroupController {

    @Resource
    private CustomerGroupService customerGroupService;

    @PostMapping("/create")
    @Operation(summary = "创建客户组管理")
    @PreAuthorize("@ss.hasPermission('mdm:customer-group:create')")
    public CommonResult<Long> createCustomerGroup(@Valid @RequestBody CustomerGroupSaveReqVO createReqVO) {
        return success(customerGroupService.createCustomerGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户组管理")
    @PreAuthorize("@ss.hasPermission('mdm:customer-group:update')")
    public CommonResult<Boolean> updateCustomerGroup(@Valid @RequestBody CustomerGroupSaveReqVO updateReqVO) {
        customerGroupService.updateCustomerGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户组管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mdm:customer-group:delete')")
    public CommonResult<Boolean> deleteCustomerGroup(@RequestParam("id") Long id) {
        customerGroupService.deleteCustomerGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户组管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mdm:customer-group:query')")
    public CommonResult<CustomerGroupRespVO> getCustomerGroup(@RequestParam("id") Long id) {
        CustomerGroupDO customerGroup = customerGroupService.getCustomerGroup(id);
        return success(BeanUtils.toBean(customerGroup, CustomerGroupRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户组管理分页")
    @PreAuthorize("@ss.hasPermission('mdm:customer-group:query')")
    public CommonResult<PageResult<CustomerGroupRespVO>> getCustomerGroupPage(@Valid CustomerGroupPageReqVO pageReqVO) {
        PageResult<CustomerGroupDO> pageResult = customerGroupService.getCustomerGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CustomerGroupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户组管理 Excel")
    @PreAuthorize("@ss.hasPermission('mdm:customer-group:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCustomerGroupExcel(@Valid CustomerGroupPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CustomerGroupDO> list = customerGroupService.getCustomerGroupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户组管理.xls", "数据", CustomerGroupRespVO.class,
                        BeanUtils.toBean(list, CustomerGroupRespVO.class));
    }

}